﻿using System.Linq;
using System.Text;
using System.Windows;
using HappyBa.Core.Data;

namespace HappyBa.Components
{
    /// <summary>
    /// Interaction logic for DetailsView.xaml
    /// </summary>
    public partial class DetailsView 
    {
        public DetailsView()
        {
            InitializeComponent();
            DataContextChanged += OnDataContextChanged;
        }

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var entity = DataContext as UIEntityBase;
            Comments.Value = entity == null ? null : BuildComments(entity);
        }

        private static string BuildComments(UIEntityBase entity)
        {
            var sEntity = entity as UIStatebleEntityBase;
            if (sEntity == null) return null;
            var sb = new StringBuilder();
            foreach (var comment in sEntity.Comments.OrderBy(x => x.CreateDate))
            {
                sb.AppendLine(string.Format("<p/><b>{0}:{1}</b><p/>", comment.CreateDate.ToShortDateString(), comment.Owner));
                sb.AppendLine(comment.Description);
            }
            return sb.ToString();
        }
    }
}
